﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ME860.DotNetty.Common
{
    public static class StringExtension
    {
        //todo:匹配全字符，验证字符串，匹配IPv4
        /// <summary>
        /// 获取完全匹配的正则表达式
        /// </summary>
        /// <param name="resStr">正则表达式</param>
        /// <returns></returns>
        public static string GetPerfectRegStr(string resStr)
        {
            if (string.IsNullOrEmpty(resStr)) return string.Empty;
            const char frist = '^';
            const char last = '$';
            if (!resStr[0].Equals(frist)) resStr = frist + resStr;
            if (!resStr[resStr.Length - 1].Equals(last)) resStr += last;
            return resStr;
        }

        /// <summary>
        /// 验证字符串
        /// </summary>
        /// <param name="inputStr">输入字符串</param>
        /// <param name="resStr">正则字符串</param>
        /// <returns></returns>
        public static bool VerifyRegex(this string inputStr, string resStr)
        {
            return !string.IsNullOrEmpty(inputStr) && !string.IsNullOrEmpty(resStr) && Regex.IsMatch(inputStr, resStr);
        }
        /// <summary>
        /// 验证字符串正则
        /// </summary>
        /// <param name="inputStr">输入字符串</param>
        /// <param name="resStr">正则字符串</param>
        /// <param name="isPerfect">是否全字匹配</param>
        /// <returns></returns>
        public static bool VerifyRegex(this string inputStr, string resStr, bool isPerfect)
        {
            resStr = isPerfect ? GetPerfectRegStr(resStr) : resStr;
            return VerifyRegex(inputStr, resStr);
        }

        /// <summary>
        /// 验证IPv4
        /// </summary>
        /// <param name="inputStr">输入字符串</param>
        /// <returns></returns>
        public static bool IsIPv4(this string inputStr)
        {
           return VerifyRegex(inputStr, RegexData.InternetProtoclV4, true);
        }



    }
}
